import Game

deck,board,num,end = Game.PokerDeck(),Game.Board(0),None,False #Globals used throughout
deck.shuffle()

board.place(deck.draw(),0)
top = board.contents()[-1]

while num == None: num = Game.Prompter("How many players are there?",range(7)[2:]).getVal()
players = Game.initPlayers(num,board,('hand'),())

num = 1
for p in players:
    p.addItems(deck.draw(5))
    p.setName("Player " + str(num))
    num += 1

def winner():
    for p in players:
        if len(p.getContents()) == 0:
            return p
    return None

def valid(card):
    if len(board.contents()) == 0: return True
    return card.suit == top.suit or card.value == top.value or card.rank == "8"

print ("The top card is " + str(top))
while not end:
    for p in players:
        p.sortItems(Game.sortPokerCards)
        end = winner() != None
        if not end:
            if Game.suitmatch(p.getContents(),top.suit) or Game.rankmatch(p.getContents(),top.rank) or Game.rankmatch(p.getContents(),"8"):
                card = None
                while card == None or not valid(card): card = p.select(str(p) + ", please choose a card to play.",p.getContents())
                p.play(card,0)
                if card.rank == "8":
                    suit = None
                    while suit == None: suit = p.select("It's an eight! Select the suit to play!",['Diamonds','Clubs','Hearts','Spades'])
                    board.place(Game.Card(suit,-1,"8"),0)
                top = board.contents()[-1]
                print ("The top card is " + str(top))
            else:
                if len(deck.cards) != 0: p.add(deck.draw())
                else:
                    for x in board.contents():
                        if x != top: deck.addCard(x)
                    deck.shuffle()
print(str(winner()) + " wins!")